package br.mil.pamerj.projects.portal.portlets;
import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ImageLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.tomasio.projects.organization.webservices.EfetivoWS;
import com.tomasio.projects.organization.webservices.EfetivoWSProxy;
import com.tomasio.projects.organization.webservices.FotoDTO;
import com.tomasio.projects.organization.webservices.MatriculaDTO;
import com.tomasio.projects.organization.webservices.PessoaDTO;
import com.tomasio.projects.organization.webservices.TelefoneDTO;


public class ImportOrganizationPortlet extends MVCPortlet {

	@Override
	public void processAction(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		// TODO Auto-generated method stub
		super.processAction(actionRequest, actionResponse);
		try {
			List<User> users = UserLocalServiceUtil.getUsers(1, 1000);
			
			EfetivoWS proxy = new EfetivoWSProxy();
//			
			for (User u : users) {
				

				String s[] = u.getEmailAddress().split("@");
				String login = s[0];
				System.out.println(login);
				
				MatriculaDTO matricula = proxy.findMatriculaByLogin(login);
				
				if (matricula != null) {
					PessoaDTO pessoa = proxy.findPessoa(matricula.getPessoa()
							.getId());
					if(pessoa != null) {
						System.out.println(pessoa.getNome());
						u.setJobTitle(matricula.getCargo());
						u.getBirthday().setTime(
								pessoa.getDataNascimento().getTimeInMillis());
						
						TelefoneDTO [] telefones = pessoa.getTelefones();
						if(telefones != null){
							
							for(int i=0; i < telefones.length; i++){
								System.out.println("interação" + i);
								TelefoneDTO telefone = telefones[i];
								if(telefone.getNumero().contains("2117")){
									u.setOpenId(
											telefone.getNumero().substring(5)
											);
									
								}
							}
						}

						
						
						FotoDTO foto = null; 
						if(pessoa.getFotos() != null){
							foto = pessoa.getFotos()[0];
						}
						if(foto != null){
							Long imageId = u.getPortraitId();
							if(imageId != null && imageId > 0){
								ImageLocalServiceUtil.updateImage(imageId,
										foto.getSource());
							}else{
					
								
								//Image image = ImageLocalServiceUtil.getImage(foto.getSource());
								Long pk = System.currentTimeMillis();
								ImageLocalServiceUtil.createImage(imageId);
								ImageLocalServiceUtil.updateImage(pk, foto.getSource());
								
								u.setPortraitId(pk);
							}
						}
						
						UserLocalServiceUtil.updateUser(u);
						
					}
				}
			}
			
			
		} catch(PortalException e){
			e.printStackTrace();
		} catch (SystemException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
